package com.unicom.priceboard.data.hastc;

import java.io.IOException;
import java.io.InputStream;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.unicom.priceboard.data.BaseXMLParser;
import com.unicom.priceboard.thread.HaStockThread;
import com.unicom.priceboard.data.vo.BestPriceInfo;
import com.unicom.priceboard.data.vo.HaStock;

public class HaStockParser extends BaseXMLParser{
	//for storing Node Value
	private StringBuffer tempVal;
	
	//to maintain context
	private HaStock tempStock;
	public void parseDocument(String xmlFile) {
		super.parseDocument(xmlFile);				
	}
	
	public void parseDocument(InputStream is) throws IOException{
		super.parseDocument(is);
	}


	//Event Handlers
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		//reset
		tempVal = new StringBuffer("");
		if(qName.equalsIgnoreCase("STS_STOCKS_INFO")) {
			tempStock = new HaStock();
		}		
	}
	

	public void characters(char[] ch, int start, int length) throws SAXException {
		tempVal.append(ch,start,length);
	}
	
	public void endElement(String uri, String localName, String qName) throws SAXException {

		if(qName.equalsIgnoreCase("STS_STOCKS_INFO")) {
			//add it to the list
			if ((tempStock.getStockType() == 2)){
					//get the best bids and offers of current Stock from the parser
					BestPriceInfo oBestPrice = BestPriceParser.getLstBestPrice().get(tempStock.getStockId());
					if(oBestPrice!=null)
						tempStock.setBestPrice(oBestPrice);
					//Compare with the existing stock in the list
					HaStock stockInList = HaStockThread.currentStockList.get(tempStock.getSymbol());
					if(stockInList==null || stockInList.equals(tempStock)==false) {
						double curChange = 0;
						double close_change = 0;
						double refPriceVal = 0;
						
						//chia co tuc o san hastc
						if(tempStock.getPriorClosePrice()!=0){
							 tempStock.setShare_Dividend("0");//khong chia co tuc
							 if(tempStock.getPriorClosePrice()>tempStock.getCeiling()||tempStock.getPriorClosePrice()<tempStock.getFloor()){
								 tempStock.setShare_Dividend("1");
								 refPriceVal = (tempStock.getCeiling()+tempStock.getFloor())/2;
							 }else {
								 refPriceVal = tempStock.getPriorClosePrice();
							 }
						}
						if(tempStock.getPriorClosePrice()==0){
							curChange=0;
						}else if(tempStock.getPrice()!=0) {
							curChange = tempStock.getPrice()-refPriceVal;
						}
						//---nguyen sua cho nay
						double curPct_change = 0;
						if(refPriceVal != 0){
							curPct_change = (curChange/refPriceVal)*100;
						} else {
							curPct_change = 0;
						}
						//###nguyen sua cho nay
						tempStock.setChange(curChange/1000);
						tempStock.setPct_change(curPct_change);
						
						double close_pctChange = 0;
						if(tempStock.getAveragePrice()!=0 && tempStock.getPriorClosePrice()!=0) {
							close_change = tempStock.getAveragePrice()-refPriceVal;
							//---nguyen sua cho nay
							if(refPriceVal != 0){
								close_pctChange  = (close_change/refPriceVal)*100;
							} else {
								close_pctChange  = 0;
							}
							//###nguyen sua cho nay
						}
						tempStock.setClose_change(close_change/1000);
						tempStock.setClose_pct_Change(close_pctChange);
						
						// Original put stock to list here, but we need all so move to 107
					}else {
						//SBSData_adapter.changeHaStatus(tempStock.getSymbol(), false);
					}					
			} else {
				if (tempStock.getPrice() != 0) {
					tempStock.setChange(tempStock.getPrice() - tempStock.getPriorClosePrice());
					if (tempStock.getPriorClosePrice() != 0) {
						tempStock.setPct_change(tempStock.getChange()/tempStock.getPriorClosePrice() * 100);
					}
				} 
			}
			HaStockThread.currentStockList.put(tempStock.getSymbol(), tempStock);
		} else if (qName.equalsIgnoreCase("TRADING_DATE")) {
			if (tempVal.length() >= 10) {
				tempStock.setTradingDate(tempVal.substring(0, 10));
			}
		} else if (qName.equalsIgnoreCase("STOCK_ID")) {
			tempStock.setStockId(Integer.parseInt(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("CODE")) {
			tempStock.setSymbol(tempVal.toString());
		} else if (qName.equalsIgnoreCase("STOCK_TYPE")) {
			tempStock.setStockType(Integer.parseInt(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("BASIC_PRICE")) {
			tempStock.setPriorClosePrice(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("CEILING_PRICE")) {
			tempStock.setCeiling(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("FLOOR_PRICE")) {
			tempStock.setFloor(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("NM_TOTAL_TRADED_QTTY")) {
			tempStock.setTotalVol(Integer.parseInt(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("MATCH_PRICE")) {
			tempStock.setPrice(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("MATCH_QTTY")) {
			tempStock.setVol(Integer.parseInt(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("AVERAGE_PRICE")) {
			tempStock.setAveragePrice(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("PREV_PRIOR_PRICE")) {
			tempStock.setPrevPrice(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("TOTAL_TRADING_VALUE")) {
			tempStock.setTotalTradingValue(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("TOTAL_TRADING_QTTY")) {
			tempStock.setTotalTradingVolume(Integer.parseInt(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("HIGHEST_PRICE")) {
			tempStock.setHighestPrice(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("LOWEST_PRICE")) {
			tempStock.setLowestPrice(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("NAME")) {
			tempStock.setStock_name(tempVal.toString());
		} else if (qName.equalsIgnoreCase("OPEN_PRICE")) {
			tempStock.setOpenPrice(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("CLOSE_PRICE")) {
			tempStock.setClosePrice(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("TIME")) {
			tempStock.setTime(tempVal.toString());
		} else if (qName.equalsIgnoreCase("SELL_COUNT")) {
			tempStock.setTotalDeal(Short.parseShort(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("BUY_FOREIGN_QTTY")) {
			tempStock.setForeignBuyVol(Integer.parseInt(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("BUY_FOREIGN_VALUE")){
			tempStock.setForeignBuyVal(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("SELL_FOREIGN_QTTY")) {
			tempStock.setForeignSellVol(Integer.parseInt(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("SELL_FOREIGN_VALUE")) {
			tempStock.setForeignSellVal(Double.parseDouble(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("PT_TOTAL_TRADED_QTTY")) {
			tempStock.setPtdTotalVolume(Integer.parseInt(tempVal.toString()));
		} else if (qName.equalsIgnoreCase("PT_TOTAL_TRADED_VALUE")) {
			tempStock.setPtdTotalValue(Double.parseDouble(tempVal.toString()));
		}	
//		} else if (qName.equalsIgnoreCase("TOTAL_BUY_TRADING_QTTY")) {
//			tempStock.setTotalBuyVol(Integer.parseInt(tempVal.toString()));
//		} else if (qName.equalsIgnoreCase("TOTAL_BUY_TRADING_VALUE")) {
//			tempStock.setTotalBuyVal(Double.valueOf(tempStock.toString()));
//		} else if (qName.equalsIgnoreCase("TOTAL_SELL_TRADING_QTTY")) {
//			tempStock.setTotalSellVol(Integer.parseInt(tempVal.toString()));
//		} else if (qName.equalsIgnoreCase("TOTAL_SELL_TRADING_VALUE")) {
//			tempStock.setTotalSellVal(Double.parseDouble(tempVal.toString()));
		
		
	}
}